<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>游记明细</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/plugins/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/plugins/jquery/jquery.min.js"></script>
    <script src="/js/plugins/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/travelContent.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <script src="/js/common.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css">
    <script src="/js/plugins/dialog2/dialog.min.js"></script>

    <link rel="stylesheet" href="/css/strategyComment.css"/>
    <script src="/js/plugins/jquery-confirm/js/jquery-confirm.js"></script>
    <link rel="stylesheet" href="/js/plugins/jquery-confirm/css/jquery-confirm.css">
    <script src="/js/common.js"></script>
    <script src="/js/plugins/easyEditor/easyEditor.min.js"></script>

    <script>
        //点赞用的隐藏域变量
        var userId;

        //删除用的id
        var id;
        var relationId;

        //点赞数量参数
        var likeNumber;

        /**
         * 1代表点过赞了
         * 2代表没点赞(已经取消点赞了)
         */
        var isUpvokeFlag;

        //查询粉丝数的方法
        function queryFans(concernedId) {
            $.get("/concerneds/" + concernedId, function (data) {
                $('#fans').html(data);
            })
        }

        //取消点赞的方法
        function deleteUpvoke(id, relationId) {
            $.ajax({
                url: '/upvokes/travles/' + id + '/' + relationId,
                type: "DELETE",
                success: function (data) {
                    isUpvokeFlag=0;
                    $("#likeBtn").addClass("fa-thumbs-o-up")//实心
                    $("#likeBtn").removeClass("fa-thumbs-up")//空心
                }
            });
        }
        var userId;
        $(function () {
            var concernedId = 0;
            var params = getParams();
            if(params.id){
                //获取游记相关信息
                $.ajaxSettings.async = false;
                $.get('/travels/' + params.id, function (data) {
                    userId =data.author.id;

                    var json = {
                        showNum: data.showNum
                    }
                    $('#show').renderValues(json);
                    //点赞数 likeNum
                    userId = data.author.id;
                    console.log(userId);
                    concernedId = data.author.id;
                    likeNumber=data.likeNum;
                    $(".likeNum").html(data.likeNum);
                    $(".travels").renderValues(data, {
                        getPerson: function (item, value) {
                            var html;
                            if(value==1){
                                html = '一个人的旅行';
                            }else if(value==2){
                                html = '和父母';
                            }else if(value==3){
                                html = '和朋友';
                            }else if(value==4){
                                html = '和同事';
                            }else if(value==5){
                                html = '和爱人';
                            }else if(value==6){
                                html = '和其他';
                            }
                            $(item).html(html);
                        }
                    });
                })
                $.get("/upvokes/" + params.id + "/" + userId, {type: 1}, function (data) {
                    if (data.success) {
                        if (data.result == null) {//没点过赞
                            isUpvokeFlag = 0;
                            $("#likeBtn").addClass("fa-thumbs-o-up");
                            $("#likeBtn").removeClass("fa-thumbs-up")
                        } else {//实心刷新之前是点了赞的
                            id=data.result.id
                            relationId = data.result.relationId
                            $("#likeBtn").addClass("fa-thumbs-up");
                            $("#likeBtn").removeClass("fa-thumbs-o-up")
                            isUpvokeFlag = 1;
                        }
                    }
                });

                $.ajaxSettings.async = true;
                queryFans(concernedId)

                //收藏功能
                $.get('/collects',{"travel.id":params.id},function (data) {
                    if (!data.success) {
                       $('#collectBtn').removeClass("fa-star-o");
                        $('#collectBtn').addClass("fa-star");
                    }
                })
            }
            //查询到当前登录用户所关注的所以用户id
            $.get("/concerneds",function (data) {
                var num = $.inArray(concernedId, data);
                if (num != -1){
                    $('#followBtn').html("已关注");
                    //消除点击事件
                    $("#followBtn").unbind("click");
                }
            })

            $('#followBtn').click(function () {
                $.post('/concerneds/'+concernedId,function (data) {
                    if(data.success){
                        queryFans(concernedId)
                        $(document).dialog({
                            type : 'notice',
                            infoText: '关注成功',
                            autoClose: 500,
                            position: 'center'  // center: 居中; bottom: 底部
                        });
                        $('#followBtn').html("已关注");
                        //消除点击事件
                        $("#followBtn").unbind("click");
                    }else {
                        $(document).dialog({
                            type : 'notice',
                            infoText: data.msg,
                            autoClose: 1500,
                            position: 'center'  // center: 居中; bottom: 底部
                        });
                    }
                })
            });

            //收藏游记的点击事件
            $('#collectBtn').click(function () {
                if ($('#collectBtn').hasClass("fa-star")) {
                    $.ajax({
                        url : "/collects" ,   // 处理的请求路径
                        type : "post" ,      // 此处发送的是delete请求（可变更为其他需要的请
                        data:{_method:"DELETE","travel.id":params.id},
                        dataType : "json" , // 返回的数据类型为json类型
                        success : function(data) {
                            $('#collectBtn').removeClass("fa-star");
                            $('#collectBtn').addClass("fa-star-o");
                        }
                    }) ;
                    $(document).dialog({
                        type : 'notice',
                        infoText: '取消收藏成功',
                        autoClose: 500,
                        position: 'center'  // center: 居中; bottom: 底部
                    });
                }else {
                    $.post('/collects/travel',{"travel.id":params.id},function (data) {
                        if(data.success){
                            $(document).dialog({
                                type : 'notice',
                                infoText: '收藏成功',
                                autoClose: 500,
                                position: 'center'  // center: 居中; bottom: 底部
                            });
                            $('#collectBtn').removeClass("fa-star-o");
                            $('#collectBtn').addClass("fa-star");
                        }else {
                            $(document).dialog({
                                type : 'notice',
                                infoText: data.msg,
                                autoClose: 1500,
                                position: 'center'  // center: 居中; bottom: 底部
                            });
                        }
                    })
                }

            })

            //点赞功能
            $("#likeBtn").click(function () {
                if(sessionStorage.getItem("user")==null){
                    $(document).dialog({
                        type: 'notice',
                        infoText: '您还没登录,请先登录! ',
                        autoClose: 500,
                        position: 'center'  // center: 居中; bottom: 底部
                    });
                    return;
                }
                //点赞
                if ($("#likeBtn").hasClass("fa-thumbs-o-up")) {
                    //isUpvokeFlag == 0 &
                    $.post('/upvokes/' + userId + '/travels', {relationId: params.id}, function (data) {
                        id = data.result.id
                        relationId = data.result.relationId;
                        if (data.success) {
                            $("#likeBtn").addClass("fa-thumbs-up");
                            $("#likeBtn").removeClass("fa-thumbs-o-up")
                            isUpvokeFlag=1;
                            likeNumber++;
                            $(".likeNum").html(likeNumber)
                        }

                    })
                }else{
                    deleteUpvoke(id, relationId);
                    likeNumber--;
                    $(".likeNum").html(likeNumber)
                }
            });
            //跳转评论页面
           /* $('#commentBtn').click(function () {
                window.location.href = "/mine/travelContentReply.html?travelId=" + params.id;
            })*/
            //当前页的input框
            var currentInput = $("#currentPage");
            //游记评论数组
            var travelContentArr = [];

            //总页数
            var pages = 1;

            function travelContentQuery() {
                //获取当前页的值
                var currentPage = currentInput.val();//1
                //查询评论数据
                $.get('/travelContentReplies/replies', {
                    travelId: params.id,
                    currentPage: currentPage
                }, function (data) {
                    //合并两个数组到第一个参数数组上
                    $.merge(travelContentArr, data.list);
                    var json = {
                        list: travelContentArr
                    }
                    $('.reply').renderValues(json);
                    //设置总页数
                    pages = data.pages;
                })
                //当前页input框+1
                currentInput.val(parseInt(currentPage) + 1);
            }

            //默认查询第一页的数据
            travelContentQuery();

            //当页面滚动条变化时，执行的函数
            $(window).scroll(function () {
                if ($(document).scrollTop() + $(window).height() >= $(document).height()) {
                    var currentPage = currentInput.val();
                    //判断是否小于或等于最后一页,如果是就去查询
                    if (currentPage <= pages) {
                        travelContentQuery();
                    } else {
                        $(document).dialog({
                            type: 'notice',
                            infoText: '已经到底了!',
                            autoClose: 1000
                        });

                    }
                }
            });


            //查询当前游记评论数量
            $.get('/travelContentReplies/commentNum', {travelId: params.id}, function (data) {
                var json = {
                    num: data
                }
                $('.count').renderValues(json);
            })


            //实例化一个easyEditor编辑器
            var editor = easyEditor('editor');
            //渲染完页面之后,点击按钮添加评论
            $("#sendLetterBtn").click(function () {
                //var content = $(".myCommont").val();
                //获取发送内容
                var data = editor.getContent({
                    emojiSign: ['[', ']'], //表情分隔符
                    blockSign: '%' //行块分隔符
                });
                var content = data.text
                $.post('/travelContentReplies', {
                    content: content,
                    'travel.id':params.id
                }, function (data) {
                    if (data.success) {
                        window.parent.location.reload();
                    }
                })
            })



            //查询用户的游记数量
            $.get('/travels/travelNum/{userId}',{'userId':userId},function (data) {
                $('#travelNum').html(data)
            })
        })
    </script>
    <style>
        .footer {
            height: 40px;
            width: 100%;
            background-color: #f8f9fa;
            position: fixed;
            bottom: 0;
            margin-bottom: 0px;

        }

        div {
            word-wrap: break-word;
            word-break: normal;
        }
    </style>

</head>
<body>

<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left"></i></span>
            </a>
        </div>
        <div class="col">
        </div>
    </div>
</div>

<div class="travels">
    <img class="cover" render-src="coverUrl">
    <div class="container">
        <h6 class="title" render-html="title"></h6>
        <small>
            <span render-html="createTime"></span>
            浏览 <span id="show" render-html="showNum"></span>
            <!--回复 <span> 0</span>-->
        </small>
        <div class="row">
            <div class="col-2 img">
                <img  class="rounded-circle" render-src="author.headImgUrl">
            </div>
            <div class="col-6 mine">
                <small>
                    <span render-html="author.nickName"></span>
                    <button class="btn" id="followBtn">关注</button>
                    <br>
                    <span id="travelNum"> </span>篇游记
                    <span id="fans"></span>粉丝
                </small>
            </div>
            <div class="attention col">
                <span class="addr" render-html="place.name" ></span>
            </div>
        </div>
    </div>
    ,
    <div class="container">
        <fieldset>
            <legend>我的出行信息</legend>
            <div class="d-flex">
                <div class="p-2  flex-fill">
                    <i class="fa fa-calendar"></i> 出发时间 <span render-html="travelTime"></span>
                </div>
                <div class="p-2  flex-fill"><i class="fa fa-money"></i> 人均费用
                    <span render-html="perExpends"></span></div>
            </div>
            <div class="d-flex">
                <div class="p-2  flex-fill">
                    <i class="fa fa-user-circle"></i> 人物
                    <span render-key="person" render-fun="getPerson"></span>
                </div>
                <div class="p-2  flex-fill"><i class="fa fa-clock-o"></i> 出行天数
                    <span render-html="days"></span>
                </div>
            </div>
        </fieldset>

        <div class="content" render-html="travelContent.content">

        </div>
        <div class="count d-flex justify-content-between">
            <div class="p-2">评论<span render-html="num"></span></div>
        </div>
    </div>

    <hr>

    <div class="panel panel-default" style="margin-bottom: 50px">
        <input type="hidden" name="currentPage" id="currentPage" value="1">
        <div class="panel-body">
            <div class="reply">
                <div class="container" render-loop="list">
                    <div class="row">
                        <div class="col-2 comment-head">
                            <a href="userProfiles.html" data-id="43">
                                <img class="rounded-circle" style="width: 30px" render-src="list.user.headImgUrl">
                            </a>
                        </div>
                        <div class="col">
                            <span class="comment"><i render-html="list.user.nickName"></i></span>
                            <span class="comment-date" render-html="list.createTime"></span>
                            <div class="comment-content">
                                <p render-html="list.content"></p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="footer">
        <div class="d-flex justify-content-between">
            <div class="p-2  flex-fill">
                <i class="fa fa-thumbs-o-up likeNum" id="likeBtn" render-html="likeNum"> </i>
            </div>
            <div class="p-2  flex-fill"><i class="fa  fa-commenting-o"> </i>
                <a data-toggle="modal" data-target="#myModal">
                    <div class="p-2" id="commentReply"><i class="fa fa-commenting-o"></i></div>
                </a>
            </div>
            <div class="p-2  flex-fill"><i class="fa fa-star-o" id="collectBtn"> </i>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-body">
                <div id="editor" style="width:100%;height: 200px"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal" id="closeModelBtn">关闭</button>
                <button type="button" class="btn btn-primary" id="sendLetterBtn">评论</button>
            </div>
        </div>
    </div>
</div>



</body>

</html>